﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRJ_LDW_Eutoliso.AccountAdmin.Noticia
{
    public partial class Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                try
                {

                    string idNoticia = Request.QueryString["Id"].Trim();

                    eutolisodbEntities1 context = new eutolisodbEntities1();
                    tolisoASPNETDBEntities dbApsUser = new tolisoASPNETDBEntities();

                    int id_Noticia = Convert.ToInt32(idNoticia);

                    var _query = (from n in context.lisonoticias

                                  //join u in dbApsUser.Users
                                  //on n.Usuarios equals u.UserId
                                  where n.Id == id_Noticia
                                  select new
                                  {

                                      
                                      n.Titulo,
                                      n.Imagem1,
                                      n.Videolink,
                                      n.Categorias,
                                      n.Data,
                                      n.DescricaoGr,
                                      n.DescricaoPq,
                                      //usuario = u.UserName

                                  });


                    foreach (var Result in _query)
                    {


                        txtCategoria.Text = Result.Categorias;
                        txtDescBreve.Text = Result.DescricaoPq;
                        txtDescGrande.Text = Result.DescricaoGr;
                        txtTitulo.Text = Result.Titulo;
                        Image1.ImageUrl = Result.Imagem1.ToString();
                        txtVideo.Text = Result.Videolink;
                        txtDate1.Text = Convert.ToString(Result.Data);
                        
                       
                        Image1.Width = 200;
                        Image1.Height = 200;

                    }


                }
                catch (Exception)
                {


                }

            }
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {

            try
            {

                using (eutolisodbEntities1 context = new eutolisodbEntities1())
                {
                    string idNoticia = Request.QueryString["Id"].Trim();

                    
                    tolisoASPNETDBEntities dbApsUser = new tolisoASPNETDBEntities();

                    int id_Noticia = Convert.ToInt32(idNoticia);

                    var _query = (from n in context.lisonoticias

                                  //join u in dbApsUser.Users
                                  //on n.Usuarios equals u.UserId
                                  where n.Id == id_Noticia
                                  select n).First();


                    if (FileUploadControl.HasFile)
                    {
                        try
                        {
                            if (FileUploadControl.PostedFile.ContentType == "~/Images/jpeg")
                            {
                                if (FileUploadControl.PostedFile.ContentLength < 102400)
                                {
                                    string filename = System.IO.Path.GetDirectoryName(FileUploadControl.FileName);
                                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                                    StatusLabel.Text = filename;
                                }
                                else
                                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                            }
                            else
                                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                        }
                        catch (Exception ex)
                        {
                            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                        }
                    }


                    _query.Titulo = txtTitulo.Text;
                    _query.Data = Convert.ToDateTime( txtDate1.Text);
                    _query.DescricaoPq = txtDescBreve.Text;
                    _query.DescricaoGr = txtDescGrande.Text;
                    _query.Videolink = txtVideo.Text;
                    _query.Categorias = txtCategoria.Text;
                    _query.Imagem1 = "~/Images/" + FileUploadControl.FileName;

                    context.SaveChanges();

                    Image1.ImageUrl = _query.Imagem1;
                    Image1.Width = 200;
                    Image1.Height = 200;

                    lblMsg.Visible = true;        

                    
                }

            }
            catch (Exception)
            {
                
                throw;
            }

        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }





    }
}